package cn.edu.nhic.controller;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * @Description
 * @Date 2024/9/23 14:40
 * @Created by dwp
 */
@WebServlet(urlPatterns = "/share_data")
public class ShareDataServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setAttribute("username", "admin");
        req.setAttribute("password", "admin");

        HttpSession session = req.getSession();
        session.setAttribute("username", "zhangsan");
        session.setAttribute("password", "zhangsan");

        ServletContext servletContext = req.getServletContext();
        servletContext.setAttribute("username", "lisi");
        servletContext.setAttribute("password", "wangwu");

        PrintWriter writer = resp.getWriter();
        writer.print("share_data page\n");
//        writer.flush();
//        writer.close();

        /**
         * forward: 请求转发：地址栏链接没有变化，页面内容为转发之后的页面内容
         * include: 请求包含: 地址栏链接没有变化,页面内容为 当前页面的内容 + 转发之后的页面内容
         * sendRedirect： 重定向：地址栏连接发生变化。内容为跳转之后新页面的内容
         */
//        req.getRequestDispatcher("share_data.jsp").include(req, resp);
//        req.getRequestDispatcher("share_data.jsp").forward(req, resp);

        resp.sendRedirect("share_data.jsp");
    }
}
